<?php

namespace App\Http\Controllers\Admin;

use App\Models\Role;
use Illuminate\Http\Request;
use App\Models\Node;

class RoleController extends BaseController
{
    /**\RoleController
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //获取搜索
        $kw = $request->input('name','');
        $stime = $request->input('stime','');
        $etime = $request->input('etime','');
        //when 参数1,变量存在则执行参数2--->匿名函数
//        \DB::connection()->enableQueryLog();
        $data = Role::when($kw,function ($query) use ($kw){
            $query->where('name','like',"%{$kw}%");
        })->when($stime,function ($query) use ($stime,$etime){
            $query->whereBetween('created_at',[$stime.' 00:00:00',$etime.' 23:59:59']);
        })->paginate($this->pagesize);
//        print_r(\DB::getQueryLog());
        //分页
        return view('admin.role.index',compact('data','kw','stime','etime'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.role.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //异常处理
        try{
            $this->validate($request,[
                'name'=>'required|unique:roles,name',
            ]);
        }catch (\Exception $e){
            return ['status'=>1000,'msg'=>'验证不通过'];
        }
        Role::create($request->only('name'));
        return ['status'=>0,'msg'=>'添加角色成功'];
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $model = Role::find($id);
        return view('admin.role.edit',compact('model'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, int $id)
    {
        //异常处理
        try{
            $this->validate($request,[
                //unique:表名,唯一字段,[排除行的值，以哪个字段来排除]
                'name'=>'required|unique:roles,name,'.$id . ',id' //排除id=$id的那行名为name的字段值
            ]);
        }catch (\Exception $e){
            return ['status'=>1000,'msg'=>'验证不通过'];
        }
        //修改角色入库
        Role::where('id',$id)->update($request->only('name'));
//        Role::where([
//            ['id','=',$id]
//        ])->update($request->only('name'));  //数组写法少写where

        return ['status'=>0,'msg'=>'修改用户成功'];
    }

    //给角色分配权限
    public function node(Role $role)
    {
        //读取所有的权限
        $nodeAll = (new Node())->getAllList();
        //读取当前角色所拥有的权限
        $nodes = $role->nodes()->pluck('id')->toArray();
        return view('admin.role.node',compact('role','nodeAll','nodes'));

    }
    //分配处理
    public function nodeSave(Request $request,Role $role)
    {
        $role->nodes()->sync($request->get('node'));
        return redirect(route('admin.role.node',$role));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
